package com.xff.util;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
@Order(100)
public class ServiceUtil {

    @Pointcut("execution(public * com.xff.controller.PersonController.save(..))")
    public void myPointCut(){}

    @Pointcut("execution(* *(..))")
    public void mypointCut1(){}

    @Before(value = "myPointCut()")
    private static void start(JoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        System.out.println("Service---"+signature.getName()+"方法开始执行: 参数是"+Arrays.asList(args));
//        System.out.println("signature.getDeclaringType()="+signature.getDeclaringType());
//        System.out.println("signature.getDeclaringTypeName()="+signature.getDeclaringTypeName());
//        System.out.println("signature.getModifiers()="+signature.getModifiers());
    }

    @AfterReturning(value = "myPointCut()",returning = "result")
    public static void stop(JoinPoint joinPoint,Object result){
        Signature signature = joinPoint.getSignature();
        System.out.println("Service---"+signature.getName()+"方法执行结束: 结果是"+result);
    }

    @AfterThrowing(value = "myPointCut()",throwing = "e")
    public static void logException(JoinPoint joinPoint,Exception e){
        Signature signature = joinPoint.getSignature();
        System.out.println("Service---"+signature.getName()+"方法抛出异常："+e.getMessage());
    }

    @After("myPointCut()")
    public static void logFinally(JoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        System.out.println("Service---"+signature.getName()+"方法执行结束....over");
    }
   /* @Around("myPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        Signature signature = point.getSignature();
        Object[] args = point.getArgs();
        System.out.println("Service---"+"环绕通知："+signature.getName()+"方法开始执行，参数是："+ Arrays.asList(args));
        Object result=null;
        try {
            point.proceed(args);
            result=100.2;
            System.out.println("Service---"+"环绕通知："+signature.getName()+"方法返回结果："+result);
        } catch (Throwable throwable) {
            *//*throwable.printStackTrace();*//*
            System.out.println("Service---"+"环绕通知："+signature.getName()+"抛出异常");
            throw throwable;

        }finally {
            System.out.println("Service---"+"环绕通知："+signature.getName()+"执行结束....over");
        }
        return result;
    }*/
}
